﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace SPALM.SharePointSoftwareFactory.Help3Installer
{
    class Program
    {
        static void Main(string[] args)
        {
            //read from registry path to helpmanager.exe and start it
            //For /F "tokens=3-7" %%i in ('REG QUERY "HKLM\SOFTWARE\Microsoft\Help\v1.0" /v AppRoot') Do (SET thepath=%%i %%j %%k %%l)

            //"%thepath%HelpLibManager.exe" /product VS /version 100 /locale en-US /install /sourceMedia "%WORKING_DIRECTORY%HelpContentSetup.msha"


            RegistryKey startupKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Help\V1.0", true);
            if (startupKey != null)
            {
                string pathToHelpExe = startupKey.GetValue("AppRoot").ToString();
                pathToHelpExe = Path.Combine(pathToHelpExe, "HelpLibManager.exe");
                if (File.Exists(pathToHelpExe))
                {
                    Console.WriteLine("Running HelpLibManager.exe " + pathToHelpExe);

                    string pathToHelpFile = Path.Combine(GetAssemblyDirectory(), "HelpContentSetup.msha");

                    if (File.Exists(pathToHelpFile))
                    {
                        Console.WriteLine("Starting process " + pathToHelpFile);
                        ProcessStartInfo psi = new ProcessStartInfo();
                        psi.FileName = pathToHelpExe;
                        psi.Arguments = "/product VS /version 100 /locale en-US /install /sourceMedia \"" + pathToHelpFile + "\"";
                        psi.CreateNoWindow = true;
                        psi.UseShellExecute = false;
                        System.Diagnostics.Process p = new System.Diagnostics.Process();
                        p.StartInfo = psi;
                        p.Start();

                        p.WaitForInputIdle(20000);
                    }
                    else
                    {
                        Console.WriteLine("Could not find help file at location " + pathToHelpFile);
                        Console.ReadLine();
                    }
                }
                else
                {
                    Console.WriteLine("Could not find HelpLibManager.exe at location " + pathToHelpExe);
                    Console.ReadLine();
                }
            }
            else
            {
                Console.WriteLine("Could not retrieve location of HelpLibManager.exe from registry");
                Console.ReadLine();
            }
        }

        private static string GetAssemblyDirectory()
        {
            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
            UriBuilder uri = new UriBuilder(codeBase);
            string path = Uri.UnescapeDataString(uri.Path);
            return Path.GetDirectoryName(path);
        }
    }
}
